package pnu.ssm.appinfo;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class AppListHelper {
	
	private static AppListHelper instance = new AppListHelper();
	private AppListHelper() {	}
	public static AppListHelper getInstance(){ return instance; }
	private static List<AppInfo> forbiddenAppList = new ArrayList<AppInfo>();
	
	public List<AppInfo> getPackages(Context context) {
		return getInstalledApps(context, false); /* false = no system packages */
    }
	
	public List<AppInfo> getForbiddenAppList(){
		return forbiddenAppList;
	}
	
	public void setForbiddenAppList(List<AppInfo> appList){
		forbiddenAppList.clear();
		for(AppInfo info : appList){
			forbiddenAppList.add(info);
		}
	}
    
	private List<AppInfo> getInstalledApps(Context context, boolean getSysPackages) {
    	List<AppInfo> appList = new ArrayList<AppInfo>();
    	List<PackageInfo> packs = context.getPackageManager().
//    			getInstalledPackages(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
    			getInstalledPackages(PackageManager.GET_META_DATA);
    	
//    	int size = 0;
    	for(int i=0;i<packs.size();i++) {
//    		if(size++ == 10) break;
    		PackageInfo p = packs.get(i);
    		if ((!getSysPackages) && (p.versionName == null)) continue ;
    		
    		String appName = p.applicationInfo.loadLabel(context.getPackageManager()).toString();
    		String packName = p.packageName;
    		
    		appList.add(new AppInfo(appName, packName));
    	}
    	
    	return appList;
    }
}
